// Author: Puyan Lotfi
// Date: Written in June 2009
// Description: Language related code for FooScope (Syntax Highlighting,
//              hierarchy parsing, etc).

#include "languagefacilities.h"

LanguageFacilities::LanguageFacilities()
{
    m_bash       = new QsciLexerBash();
    m_batch      = new QsciLexerBatch();
    m_cmake      = new QsciLexerCMake();
    m_cpp        = new QsciLexerCPP();
    m_cs         = new QsciLexerCSharp();
    m_css        = new QsciLexerCSS();
    m_d          = new QsciLexerD();
    m_diff       = new QsciLexerDiff();
    m_fortran    = new QsciLexerFortran();
    m_html       = new QsciLexerHTML();
    m_idl        = new QsciLexerIDL();
    m_java       = new QsciLexerJava();
    m_js         = new QsciLexerJavaScript();
    m_lua        = new QsciLexerLua();
    m_make       = new QsciLexerMakefile();
    m_pascal     = new QsciLexerPascal();
    m_perl       = new QsciLexerPerl();
    m_ps         = new QsciLexerPostScript();
    m_pov        = new QsciLexerPOV();
    m_prop       = new QsciLexerProperties();
    m_py         = new QsciLexerPython();
    m_rb         = new QsciLexerRuby();
    m_sql        = new QsciLexerSQL();
    m_tcl        = new QsciLexerTCL();
    m_tex        = new QsciLexerTeX();
    m_vhdl       = new QsciLexerVHDL();
    m_xml        = new QsciLexerXML();
    m_yaml       = new QsciLexerYAML();

    //m_supportedLexers["txt"]             = m_normal;
    //m_supportedLexers["as"]              = m_actionscript;
    //m_supportedLexers["mx"]              = m_actionscript;
    //m_supportedLexers["ada"]             = m_ada;
    //m_supportedLexers["ads"]             = m_ada;
    //m_supportedLexers["adb"]             = m_ada;
    //m_supportedLexers["asm"]             = m_asm;
    //m_supportedLexers["asp"]             = m_asp;
    //m_supportedLexers["au3"]             = m_autoit;
    m_supportedLexers["sh"]              = m_bash;
    m_supportedLexers["bsh"]             = m_bash;
    m_supportedLexers["bat"]             = m_batch;
    m_supportedLexers["cmd"]             = m_batch;
    m_supportedLexers["nt"]              = m_batch;
    m_supportedLexers["c"]               = m_cpp;
    //m_supportedLexers["ml"]              = m_caml;
    //m_supportedLexers["mli"]             = m_caml;
    //m_supportedLexers["sml"]             = m_caml;
    //m_supportedLexers["thy"]             = m_caml;
    m_supportedLexers["cmake"]           = m_cmake;
    m_supportedLexers["h"]               = m_cpp;
    m_supportedLexers["hpp"]             = m_cpp;
    m_supportedLexers["hxx"]             = m_cpp;
    m_supportedLexers["cpp"]             = m_cpp;
    m_supportedLexers["cxx"]             = m_cpp;
    m_supportedLexers["cc"]              = m_cpp;
    m_supportedLexers["cs"]              = m_cs;
    m_supportedLexers["css"]             = m_css;
    m_supportedLexers["diff"]            = m_diff;
    m_supportedLexers["patch"]           = m_diff;
    m_supportedLexers["f"]               = m_fortran;
    m_supportedLexers["for"]             = m_fortran;
    m_supportedLexers["f90"]             = m_fortran;
    m_supportedLexers["f95"]             = m_fortran;
    m_supportedLexers["f2k"]             = m_fortran;
    //m_supportedLexers["hs"]              = m_haskell;
    //m_supportedLexers["lhs"]             = m_haskell;
    //m_supportedLexers["as"]              = m_haskell;
    //m_supportedLexers["las"]             = m_haskell;
    m_supportedLexers["html"]            = m_html;
    m_supportedLexers["htm"]             = m_html;
    m_supportedLexers["shtml"]           = m_html;
    m_supportedLexers["shtm"]            = m_html;
    m_supportedLexers["xhtml"]           = m_html;
    //m_supportedLexers["ini"]             = m_ini;
    //m_supportedLexers["inf"]             = m_ini;
    //m_supportedLexers["reg"]             = m_ini;
    //m_supportedLexers["url"]             = m_ini;
    //m_supportedLexers["iss"]             = m_inno;
    m_supportedLexers["java"]            = m_java;
    //m_supportedLexers["js"]              = m_javascript;
    //m_supportedLexers["kix"]             = m_kix;
    //m_supportedLexers["lsp"]             = m_lisp;
    //m_supportedLexers["lisp"]            = m_lisp;
    m_supportedLexers["lua"]             = m_lua;
    //m_supportedLexers["mak"]             = m_makefile;
    //m_supportedLexers["m"]               = m_matlab;
    //m_supportedLexers["nfo"]             = m_nfo;
    //m_supportedLexers["nsi"]             = m_nsis;
    //m_supportedLexers["nsh"]             = m_nsis;
    m_supportedLexers["pas"]             = m_pascal;
    m_supportedLexers["inc"]             = m_pascal;
    m_supportedLexers["pl"]              = m_perl;
    m_supportedLexers["pm"]              = m_perl;
    m_supportedLexers["plx"]             = m_perl;
    //m_supportedLexers["php"]             = m_php;
    //m_supportedLexers["php3"]            = m_php;
    //m_supportedLexers["phtml"]           = m_php;
    m_supportedLexers["ps"]              = m_ps;
    m_supportedLexers["properties"]      = m_prop;
    m_supportedLexers["py"]              = m_py;
    m_supportedLexers["pyw"]             = m_py;
    //m_supportedLexers["rc"]              = m_rc;
    m_supportedLexers["rb"]              = m_rb;
    m_supportedLexers["rbw"]             = m_rb;
    //m_supportedLexers["scm"]             = m_scheme;
    //m_supportedLexers["smd"]             = m_scheme;
    //m_supportedLexers["ss"]              = m_scheme;
    //m_supportedLexers["st"]              = m_smalltalk;
    m_supportedLexers["sql"]             = m_sql;
    m_supportedLexers["tcl"]             = m_tcl;
    m_supportedLexers["tex"]             = m_tex;
    //m_supportedLexers["vb"]              = m_vb;
    //m_supportedLexers["vbs"]             = m_vb;
    //m_supportedLexers["v"]               = m_verilog;
    m_supportedLexers["vhd"]             = m_vhdl;
    m_supportedLexers["vhdl"]            = m_vhdl;
    m_supportedLexers["xml"]             = m_xml;
    m_supportedLexers["xsml"]            = m_xml;
    m_supportedLexers["xsl"]             = m_xml;
    m_supportedLexers["xsd"]             = m_xml;
    m_supportedLexers["kml"]             = m_xml;
    m_supportedLexers["wsdl"]            = m_xml;
    m_supportedLexers["yml"]             = m_yaml;
}

LanguageFacilities::~LanguageFacilities()
{
    delete m_bash;
    delete m_batch;
    delete m_cmake;
    delete m_cpp;
    delete m_cs;
    delete m_css;
    delete m_d;
    delete m_diff;
    delete m_fortran;
    delete m_html;
    delete m_idl;
    delete m_java;
    delete m_js;
    delete m_lua;
    delete m_make;
    delete m_pascal;
    delete m_perl;
    delete m_ps;
    delete m_pov;
    delete m_prop;
    delete m_py;
    delete m_rb;
    delete m_sql;
    delete m_tcl;
    delete m_tex;
    delete m_vhdl;
    delete m_xml;
    delete m_yaml;
}   

QsciLexer* LanguageFacilities::getLexer(QString filename)
{
    if(!filename.contains(".", Qt::CaseInsensitive)) return NULL; // TODO

    QStringList tokens = filename.split(".", QString::SkipEmptyParts, Qt::CaseInsensitive);

    if(tokens.size() <= 0) return NULL; // TODO

    QString fileExtention = tokens.at(tokens.size() - 1);

    if(m_supportedLexers.find(fileExtention) == m_supportedLexers.end()) return NULL; // TODO

    QsciLexer* lexer =  m_supportedLexers[fileExtention];


    // XXX
    QFont font;
    font.setFamily(QString::fromUtf8("DejaVu Sans Mono"));
    font.setPointSize(10);
    lexer->setFont(font);

    return lexer;

}
